package org.ekstrabilet.web.beans;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.ekstrabilet.user.beans.User;
import org.ekstrabilet.user.logic.UserLogic;

public class RegisterBean {
	private String login;
	private String password;
	private String password2;
	private String surname;
	private String name;
	public String getLogin() {
		return login;
	}
	public void setLogin(String login) {
		this.login = login;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getPassword2() {
		return password2;
	}
	public void setPassword2(String password2) {
		this.password2 = password2;
	}
	public String getSurname() {
		return surname;
	}
	public void setSurname(String surname) {
		this.surname = surname;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	public String[] getTypes(){
		return new String[]{"Administration", "Sales service", "Technical service", "View"};
	}
	
	public String register(){
		if(!password.equals(password2)){
			FacesContext.getCurrentInstance().addMessage("registerForm:password2", new FacesMessage("Passwords are diffrent!"));
			return null;
		}
		User user= new User();
		user.setLogin(login);
		user.setName(name);
		user.setSname(surname);
		user.setPassword(password);
		user.setType("View");
		if(UserLogic.getInstance().add(user)){
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Register sucessful!"));
			return "home.xhtml?faces-redirect= true";
			
		}
		else{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Register failed!"));
			
			
		}
		return null;
		
	}
}
